<?php
namespace app\system\controller;
use app\system\controller\SystemController;
/**
 * 系统用户
 */
class SystemUserController extends SystemController {

    protected $_model = 'SystemUser';

    /**
     * 当前模块参数
     */
    protected function _infoModule(){
        return array(
            'info'  => array(
                'name' => '用户管理',
                'description' => '管理网站系统管理员',
                ),
            'menu' => array(
                    array(
                        'name' => '用户列表',
                        'url' => url('index'),
                        'icon' => 'list',
                    ),
                ),
            'add' => array(
                    array(
                        'name' => '添加用户',
                        'url' => url('add'),
                    ),
                ),
            );
    }

    /**
     * 列表
     */
    protected function _indexDataWhere($pageMaps){
        $where = array();
        $username = $pageMaps['username'];
        if(!empty($username)){
            $where[] = ' (username like "%'.$username.'%")  OR ( nicename like "%'.$username.'%") ';
        }
        $groupId = $pageMaps['group_id'];
        if(!empty($groupId)){
            $where[] = 'find_in_set('.$groupId.',group_id) ';
        }
        return $where;
    }

    /**
     * 列表
     */
    protected function _indexDataOrder() {
        return 'A.user_id desc';
    }

    protected function _indexAssign(){
        return array(
            'groupList' => target('SystemGroup')->loadList(),
            );
    }

    /**
     * 增加
     */
    protected function _addAssign(){
        return array(
            'groupList' => target('SystemGroup')->loadList(),
            );
    }

    /**
     * 修改
     */
    protected function _editAssign($info){
        return array(
            'groupList' => target('SystemGroup')->loadList(),
            'GroupArray'  => explode(',', $info['group_id']),
            );
    }

    /**
     * 删除
     */
    protected function _delBefore($id){
        if($id == 1){
            $this->error('保留用户无法删除！');
        }
    }

    /**
     * 资料
     */
    public function userData(){
        if(!IS_POST){
            $id = SYSTEM_ID;
            if(empty($id)){
                $this->error('参数不能为空！');
            }
            $model = target($this->_model);
            $info = $model->getInfo($id);
            if(!$info){
                $this->error($model->getError());
            }
            if($id <> SYSTEM_ID){
                $this->error('您无法操作其他用户!');
            }
            $this->assign('SystemMenu', target('Menu')->getMenu($loginUserInfo));
            $this->assign('quickMenu', unserialize($info['quick']));
            $this->assign('info', $info);
            $this->systemDisplay('data');
        }else{
            if(target($this->_model)->saveUser()){
                $this->success('修改成功！');
            }else{
                $msg = target($this->_model)->getError();
                if(empty($msg)){
                    $this->error('修改失败');
                }else{
                    $this->error($msg);
                }
            }
        }
    }

    /**
     * 保存验证
     */
    protected function _editAjaxBefore($id, $field, $val){
        if(SYSTEM_ID <> 1 && SYSTEM_ID <> $id){
            $this->error('您无权更改其他用户！');
        }
        if($field <> 'status' && $field <> 'remark'){
            $this->error('您无法操作其他字段！');
        }
    }

    /**
     * 搜索用户
     */
    public function searchUser() {
        $name = request('post.name');
        $where = array();
        $where[] = " username = '{$name}' or nicename = '{$name}' ";
        $info = target($this->_model)->getWhereInfo($where);
        if(empty($info)){
            $this->error('没有该用户！');
        }
        $this->success($info);
    }

    


}

